import React, { Component } from 'react';
import {
    View,
    Text,
    StyleSheet,
    Platform
} from 'react-native';

import svgs from '../getSvgs/svgs.js';
import SvgUri from 'react-native-svg-uri';
export default class Svg extends Component{
    constructor(props){
        super(props);

    }

    render() {

        const {icon, color, size, style,height,width} = this.props;
        let newad = svgs[icon];


        let svgXmlData = svgs[icon];
        if (!svgXmlData) {
            let err_msg = `没有"${icon}"这个icon`;
            throw new Error(err_msg);
        }

        if( Platform.OS !=='web'){

            console.log(SvgUri)
            return (
                <SvgUri
                    width={width ? width : size}
                    height={height ? height : size}
                    svgXmlData={svgXmlData}
                    fill={color}
                    style={style}
                />
            )
        }else{
            let regx1=/fill=\".*?\"/g;
            let regx=/\w+=\"[0-9].*?\"/g;
            let obj={
                fill:color,
                width:width ? width : size,
                height:height ? height : size
            },arr=[],arrs=[];

            newad= newad.replace(regx1,function(m,v){
                return v='fill='+color;
            })
            arr=newad.match(regx);
            newad = newad.replace(regx,function(m,v){
                for(let i in arr){
                    arrs=arr[i].match(/([^\=])+/g);
                    if(obj.hasOwnProperty(arrs[0])){
                        arr[i]= arrs[0]+'='+obj[arrs[0]]
                    }
                }
                return v=arr.join(' ');
            })

           return (<svg style={{color:{color}}} dangerouslySetInnerHTML={{__html:newad}} width={size} height={size}/>)


       }

    }
}
const styles=StyleSheet.create({
    container:{

    }
})
